package com.hsmpay.mobile.dao.brokerage.impl;

import com.hsmpay.common.dao.Impl.BaseDaoSupport;
import com.hsmpay.mobile.dao.brokerage.BrokerageOrderDao;
import com.hsmpay.mobile.pojo.brokerage.BrokerageOrder;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 佣金合单dao 接口
 * 对应的pojo 是 BrokerageOrder
 */
@Repository("brokerageOrderDao")
public class BrokerageOrderDaoImpl extends BaseDaoSupport<BrokerageOrder,Long> implements BrokerageOrderDao<BrokerageOrder,Long> {
	/**
	 * 根据ID  修改状态
	 * @param id
	 * @param status
	 * @param description
	 * @return
	 * @throws SQLException
	 */
	public int updateBrokerageOrderByStatus(Long id,Integer status,String description)throws SQLException{
		BrokerageOrder cb = new BrokerageOrder();
		cb.setId(id);
		cb.setStatus(status);
		cb.setDescription(description);
		return updateEntity(cb);
	}

	/**
	 * 根据结算IDs  查询 结算订单
	 * @param ids
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<BrokerageOrder> getBrokerageOrderListByIds(String ids)throws SQLException{
		return (List<BrokerageOrder>) getSqlMapClientTemplate().queryForList("brokerageOrderSqlMap.getBrokerageOrderListByIds",ids);
	}

	/**
	 * 根据机构层级  修改佣金状态
	 * @param paramStatus 修改状态
	 * @param description 修改描述
	 * @param status 条件状态
	 * @param layer 条件层级
	 * @param roleId 条件角色
	 * @param clearStartDate 开始结算时间
	 * @param clearEndDate 结束结算时间
	 * @return
	 * @throws SQLException
	 */
	public int updateBOStatusByLayer(Integer paramStatus,String description,Integer status,String layer,Long roleId,Date clearStartDate,Date clearEndDate,Long orderTypeId)throws SQLException{
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("paramStatus", paramStatus);
		param.put("description", description);
		param.put("status", status);
		param.put("roleId", roleId);
		param.put("layer", layer);
		param.put("clearStartDate", clearStartDate);
		param.put("clearEndDate", clearEndDate);
		param.put("orderTypeId", orderTypeId);
		
		return getSqlMapClientTemplate().update("brokerageOrderSqlMap.updateBOStatusByLayer", param);
	}
	
	/**
	 * 根据机构Id  修改父机构佣金状态
	 * @param paramStatus 修改状态
	 * @param description 修改描述
	 * @param status 条件状态
	 * @param organizationId 条件机构Id
	 * @param roleId 条件角色
	 * @param clearStartDate 开始结算时间
	 * @param clearEndDate 结束结算时间
	 * @return
	 * @throws SQLException
	 */
	public int updateBOStatusByOrganizationId(Integer paramStatus,String description,Integer status,Long organizationId,Long roleId,Date clearStartDate,Date clearEndDate,Long orderTypeId)throws SQLException{
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("paramStatus", paramStatus);
		param.put("description", description);
		param.put("status", status);
		param.put("roleId", roleId);
		param.put("organizationId", organizationId);
		param.put("clearStartDate", clearStartDate);
		param.put("clearEndDate", clearEndDate);
		param.put("orderTypeId", orderTypeId);
		
		return getSqlMapClientTemplate().update("brokerageOrderSqlMap.updateBOStatusByOrganizationId", param);
	}

	/**
	 * 批量修改发票状态
	 * @param map
	 * @throws SQLException
	 */
	public void updateInvoiceStatusByIds(Map<String,Object> map) throws SQLException{
		getSqlMapClient().update("combineBrokerageSqlMap.updateInvoiceStatusByIds",map);
	}

	/**
	 * 根据 机构 终端类型  查询 未结算的订单 
	 * @param organizationId
	 * @param roleId
	 * @param inStatus 属于的状态
	 * @param orderTypeId 订单类型
	 * @param transMoney 交易金额
	 * @return
	 * @throws SQLException
	 */
	public List<BrokerageOrder> getOIdRoleIdLessMoneyList(Long organizationId,Long roleId,String inStatus,Long orderTypeId,Double transMoney)throws SQLException{
		BrokerageOrder bo = new BrokerageOrder();
		bo.setOrganizationId(organizationId);
		bo.setRoleId(roleId);
		bo.setOrderTypeId(orderTypeId);
		bo.setOtherParam(" CB.STATUS IN("+inStatus+") AND CB.TRANSMONEY < "+transMoney);
		return searchEntityList(bo);
	}

	/**
	 * 查询该层下面的机构佣金总和（包括自己）
	 * @param layer
	 * @param roleId
	 * @param clearStartDate
	 * @param clearEndDate
	 * @param orderTypeId
	 * @return
	 * @throws SQLException
	 */
	public double getTotalMoneyByLayer(String layer, Long roleId,Date clearStartDate, Date clearEndDate, Long orderTypeId)throws SQLException {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("layer", layer);
		param.put("roleId", roleId);
		param.put("clearStartDate", clearStartDate);
		param.put("clearEndDate", clearEndDate);
		param.put("orderTypeId", orderTypeId);
		
		return (Double)getSqlMapClient().queryForObject("brokerageOrderSqlMap.getTotalMoneyByLayer", param);
	}
	
	/**
	 * 根据机构层级  修改佣金结算机构
	 * @param broClearOrg 结算机构
	 * @param layer 条件层级
	 * @param roleId 条件角色
	 * @param clearStartDate 开始结算时间
	 * @param clearEndDate 结束结算时间
	 * @return
	 * @throws SQLException
	 */
	public int updateBroClearOrgByLayer(Long broClearOrg,String layer,Long roleId,Date clearStartDate,Date clearEndDate,Long orderTypeId)throws SQLException{
		
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("broClearOrg", broClearOrg);
		param.put("layer", layer);
		param.put("roleId", roleId);
		param.put("clearStartDate", clearStartDate);
		param.put("clearEndDate", clearEndDate);
		param.put("orderTypeId", orderTypeId);
		
		return getSqlMapClientTemplate().update("brokerageOrderSqlMap.updateBroClearOrgByLayer", param);
		
	}
}
